import { Application } from "egg";
import * as querystring from "querystring";
import { ReadStream } from "fs";
import { Lockers } from "./lockers";
import * as crypto from "crypto";

/** 柜子用户管理 */
export default class CodoLockersUser {
	constructor(private app: Application) {}

	/** 上传单个用户信息
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async uploadUserInfo(addr: string, info: Lockers.Request.uploadUserInfo) {
		// this.app.valid(
		// 	{
		// 		userId: { type: "string", required: true },
		// 		userName: { type: "string", required: true },
		// 		boxList: { type: "array", itemType: "number", required: true },
		// 	},
		// 	info
		// );
		const query = querystring.stringify({
			userId: info.userId,
			userName: info.userName,
		});
		const fullPath = `${addr}/uploadUserInfoWithPhoto?${query}`;
		let buf: Buffer | ReadStream;
		if (typeof info.header === "string") {
			buf = Buffer.from(info.header, "base64");
		} else {
			buf = info.header;
			// note 处理设备和egg框架内部使用FormStream一起引发的问题，设备需要文件名后缀，如abc.jpg，FS只有stream才提供解析path，如果外部传入stream未提供path则手动加上
			// 文件名每次上传必须不同，否则设备不会更新照片
			const randomStr = crypto.randomBytes(16).toString("hex");
			buf.path = buf.path || randomStr + "a.jpg";
		}
		const res = await this.app.curl<Lockers.Response.common>(fullPath, {
			method: "POST",
			dataType: "json",
			headers: {
				"content-type": "multipart/form-data",
			},
			data: {
				boxList: JSON.stringify(info.boxList),
			},
			files: {
				header: buf,
			},
		});
		return res.data;
	}

	/** 根据用户名删除用户
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async deleteUserByName(addr: string, userName: string) {
		const fullPath = `${addr}/deleteUserByName`;
		const res = await this.app.curl<Lockers.Response.common>(fullPath, {
			method: "POST",
			dataType: "json",
			headers: {},
			data: {
				userName,
			},
		});
		return res.data;
	}

	/** 根据用户userId删除用户(接口出错)
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async deleteUserByUserId(addr: string, userId: number[]) {
		const fullPath = `${addr}/deleteUser`;
		const res = await this.app.curl<Lockers.Response.common>(fullPath, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			headers: {
				// "content-type": "multipart/form-data",
			},
			data: {
				userIdList: userId,
			},
		});
		return res.data;
	}

	/** 获取用户列表
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async getUserList(addr: string) {
		const fullPath = `${addr}/getUserList`;
		const res = await this.app.curl<Lockers.Response.GetUserInfo>(fullPath, {
			method: "GET",
			dataType: "json",
			headers: {
				// "content-type": "multipart/form-data",
			},
			data: {},
		});
		return res.data;
	}

	/** 批量绑定用户箱号(接口出错)
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async bindBoxList(addr: string, data: Lockers.Request.userBindBoxObject[]) {
		const fullPath = `${addr}/bindBoxList`;
		const res = await this.app.curl<Lockers.Response.common>(fullPath, {
			method: "POST",
			dataType: "json",
			headers: {
				"content-type": "multipart/form-data",
				// "content-type": "x-www-urlencoded",
			},
			data: {
				list: data,
			},
		});
		return res.data;
	}

	/** 批量解绑用户箱号（接口出错）
	 * @param {string} addr 设备地址 i.e. 127.0.0.1:9000
	 */
	async unbindBoxList(addr: string, data: number[]) {
		const fullPath = `${addr}/unbindBoxList`;
		const res = await this.app.curl<Lockers.Response.common>(fullPath, {
			method: "POST",
			dataType: "json",
			headers: {
				"content-type": "multipart/form-data",
				// "content-type": "x-www-urlencoded",
			},
			data: {
				list: JSON.stringify(data),
			},
		});
		return res.data;
	}
}
